import request from "@/utils/request";

import { getPublicKey } from '@/utils/publicKey';
// Rsa加密

import { encryptedRsa } from "@/utils/crypto/encrypt-rsa";


export function getUserInfo(data: any) {
  return request({
    url: "/api/v1/system/user/getInfoById",
    method: "get",
    params: data,
  });
}

export function getList(data: any) {
  return request({
    url: "/api/v1/system/user/findPage",
    method: "get",
    params: data,
  });
}

export function getListAll() {
  return request({
    url: "/api/v1/system/user/findAll",
    method: "get"
  });
}

export function getListByTenant(data: any) {
  return request({
    url: "/api/v1/system/user/findPageByTenant",
    method: "get",
    params: data,
  });
}

export function doInsert(data: any) {
  return request({
    url: "/api/v1/system/user/insert",
    method: "post",
    data,
  });
}

export function doUpdate(data: any) {
  return request({
    url: "/api/v1/system/user/update",
    method: "post",
    data,
  });
}

export function doUpdateSelf(data: any) {
  return request({
    url: "/api/v1/system/user/updateSelf",
    method: "post",
    data,
  });
}

export async function doDelete(data: any) {
  if (true) {
    // 获得公钥
    let publicKey = await getPublicKey();
    // 加密数据
    let encrypted = encryptedRsa(data, getPublicKey() || '');
    data = {
      encryptData: encrypted,
    };
  }
  return request({
    url: "/api/v1/system/user/del",
    method: "post",
    data,
  });
}

export async function doDeleteAll(data: any) {
  if (true) {
    // 获得公钥
    let publicKey = await getPublicKey();
    // 加密数据
    let encrypted = encryptedRsa(data, getPublicKey() || '');
    data = {
      encryptData: encrypted,
    };
  }
  return request({
    url: "/api/v1/system/user/delAll",
    method: "post",
    data,
  });
}

export function getRoleIdsByUserId(data: any) {
  return request({
    url: "/api/v1/system/user/getRoleIdsByUserId",
    method: "get",
    params: data,
  });
}

export function doGetRoles(data: any) {
  return request({
    url: "/api/v1/system/user/roles/getRoles",
    method: "get",
    params: data,
  });
}

export function doSetRoles(data: any) {
  return request({
    url: "/api/v1/system/user/roles/setRoles",
    method: "post",
    data,
  });
}

export function getOrgByUserId(data: any) {
  return request({
    url: "/api/v1/system/user/getOrgByUserId",
    method: "get",
    params: data,
  });
}

export function doSetOrg(data: any) {
  return request({
    url: "/api/v1/system/user/org/setOrg",
    method: "post",
    data,
  });
}

export async function doUpdatePasswordById(data: any) {
  if (true) {
    // 获得公钥
    let publicKey = await getPublicKey();
    // 加密数据
    let encrypted = encryptedRsa(data, getPublicKey() || '');
    data = {
      encryptData: encrypted,
    };
  }
  return request({
    url: "/api/v1/system/user/updatePasswordById",
    method: "post",
    data,
  });
}

export async function sendEmailCodeByEmail(data: any) {
  return request({
    url: "/api/v1/system/user/sendEmailCodeByEmail",
    method: "get",
    params: data,
  });
}


export async function resetPasswordEmailById(data: any) {
  if (true) {
    // 获得公钥
    // 加密数据
    let encrypted = encryptedRsa(data, getPublicKey() || '');
    data = {
      encryptData: encrypted,
    };
  }
  return request({
    url: "/api/v1/system/user/resetPasswordEmailById",
    method: "post",
    data,
  });
}

export async function doResetPasswordById(data: any) {
  if (true) {
    // 获得公钥
    let publicKey = await getPublicKey();
    // 加密数据
    let encrypted = encryptedRsa(data, getPublicKey() || '');
    data = {
      encryptData: encrypted,
    };
  }
  return request({
    url: "/api/v1/system/user/resetPasswordById",
    method: "post",
    data,
  });
}

export async function doEnableAccount(data: any) {
  if (true) {
    // 获得公钥
    let publicKey = await getPublicKey();
    // 加密数据
    let encrypted = encryptedRsa(data, getPublicKey() || '');
    data = {
      encryptData: encrypted,
    };
  }
  return request({
    url: "/api/v1/system/user/enableAccount",
    method: "post",
    data,
  });
}

export function doUpdateAvatar(data: any) {
  return request({
    url: "/api/v1/system/user/updateAvatar",
    method: "post",
    data,
  });
}

export async function doUpdatePassword(data: any) {
  if (true) {
    // 获得公钥
    let publicKey = await getPublicKey();
    // 加密数据
    let encrypted = encryptedRsa(data, getPublicKey() || '');
    data = {
      encryptData: encrypted,
    };
  }
  return request({
    url: "/api/v1/system/user/updatePassword",
    method: "post",
    data,
  });
}

export async function doUpdatePasswordByForget(data: any) {
  if (true) {
    // 获得公钥
    let publicKey = await getPublicKey();
    // 加密数据
    let encrypted = encryptedRsa(data, getPublicKey() || '');
    data = {
      encryptData: encrypted,
    };
  }
  return request({
    url: "/api/v1/system/user/updatePasswordByForget",
    method: "post",
    data,
  });
}

export async function doUpdateEmail(data: any) {
  if (true) {
    // 获得公钥
    let publicKey = await getPublicKey();
    // 加密数据
    let encrypted = encryptedRsa(data, getPublicKey() || '');
    data = {
      encryptData: encrypted,
    };
  }
  return request({
    url: "/api/v1/system/user/updateEmail",
    method: "post",
    data,
  });
}

export async function doUpdateMobile(data: any) {
  if (true) {
    // 获得公钥
    let publicKey = await getPublicKey();
    // 加密数据
    let encrypted = encryptedRsa(data, getPublicKey() || '');
    data = {
      encryptData: encrypted,
    };
  }
  return request({
    url: "/api/v1/system/user/updateMobile",
    method: "post",
    data,
  });
}

/**
 * 切换运营商
 * @param params
 * @returns {*}
 */
export function switchTenant(params: any) {
  return request({
    url: "/api/v1/system/user/switchTenant",
    method: "post",
    params,
  });
}

/**
 * 切换切换回自己账户
 * @param params
 * @returns {*}
 */
export function switchOneself(params: any) {
  return request({
    url: "/api/v1/system/user/switchOneself",
    method: "post",
    params,
  });
}
